//
// Created by xm on 2024/8/6.
//

#include "Test081.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;


template<class T>
class MyArray
{
public:
    // 保护原生指针，给原生指针取别名
    typedef T* iterator;
    MyArray()
    {
        mCapacity = 10;
        mSize = 10;
        p = new T[mCapacity];
        for (int i = 0; i < mCapacity; ++i) {
            p[i] = i+1;
        }
    }

    void printArray(){
        for (int i = 0; i < 10; ++i) {
            cout << p[i] << " ";
        }
    }
    // 提供迭代器，返回开始位置
    T* begin(){
        return p;
    }
    // 返回结束位置
    T*end(){
        return p + mSize;
    }

public:
    T* p;
    int mCapacity;
    int mSize;
};


template<class T>
static void printArray(T begin, T end)
{
    for (; begin != end ; ++begin) {
        cout << *begin << " ";
    }
}


static void  demo1()
{
    MyArray<int> arr;

    arr.printArray();
}

static void  demo2(){

    cout << "demo2 STL的工作机制" << endl;
    MyArray<int> arr;

    MyArray<int>::iterator begin = arr.begin();
    MyArray<int>::iterator end = arr.end();

    printArray(begin, end);

}
// 算法回调函数
static void MyPrint(int val){
    cout << val << " ";

}

static void demo3()
{
    cout << "demo3 " << endl;
    // 容器
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    // 获取开始位置的迭代器
    vector<int>::iterator begin = v.begin();
    // 获取结束位置的迭代器
    vector<int>::iterator end = v.end();

    // 遍历算法
    for_each(begin, end, MyPrint);
    cout << endl;

}
// 容器储存对象
class Maker
{
public:
    Maker(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
public:
    string name;
    int age;
};
ostream &operator<<(ostream &out, Maker &m)
{
    out << "Name:" << m.name << " Age:" << m.age << endl;
    return out;
}

static void demo4()
{
    cout << "demo4 " << endl;

    vector<Maker> v;
    //往容器中存储对象
    v.push_back(Maker("悟空", 18));
    v.push_back(Maker("小林", 19));
    v.push_back(Maker("贝吉塔", 25));
    v.push_back(Maker("龟仙人", 200));
    v.push_back(Maker("短笛", 180));

    //获取开始和结束位置的迭代器
    vector<Maker>::iterator begin = v.begin();
    vector<Maker>::iterator end = v.end();

    while (begin!=end)
    {
        cout << (*begin);
        begin++;
    }





}
// string容器

static void demo05(){
    cout << "demo05 " << endl;

    string::iterator it;


}

// vector

void printVector(const vector<int> &vec)
{
    for (vector<int>::const_iterator it = vec.begin(); it != vec.end() ; ++it) {

        cout << *it << " ";
    }

    cout << endl;

}

void printRvector(const vector<int> &vec)
{
    for (vector<int>::const_reverse_iterator it = vec.rbegin(); it != vec.rend() ; ++it) {

        cout << *it << " ";
    }

    cout << endl;

}

static void  demo06(){
    int arr[] = {3,0,4,7,1};

    vector<int> v(arr, arr + sizeof(arr)/ sizeof(int));

    printVector(v);
    printRvector(v);
}


void  test081()
{
    cout << "===== test081 =====" << endl;


}